if (typeof XMLHttpRequest == 'undefined') {
	XMLHttpRequest = function() {
		return new ActiveXObject(navigator.userAgent.indexOf('MSIE 5') >= 0 ? 'Microsoft.XMLHTTP' : 'Msxml2.XMLHTTP');
	};
}
function ajax(options) {
	options = {
		method :options.method || 'post',
		url :options.url || '',
		timeout :options.timeout || 5000,
		contentType :options.contentType || '',
		data :options.data || null,
		onComplete :options.onComplete || function() {
		},
		onError :options.onError || function() {
		},
		onSuccess :options.onSuccess || function() {
		},
		onTimeout :options.onTimeout || function() {
		},
		type :options.type || '',
		indicator :options.indicator || 'Loading...',
		showIndicator :options.showIndicator === false ? false : true 
	};

	showIndicator();

	var xml = new XMLHttpRequest();

	xml.open(options.method, options.url, true);

	var requestDone = false;

	setTimeout( function() {
		if (!requestDone) {
			requestDone = true;
			options.onTimeout();
		}
		requestDone = true;
		hideIndicator();
	}, options.timeout);

	xml.onreadystatechange = function() {
		if (xml.readyState == 4 && !requestDone) {
			requestDone = true;

			if (httpSuccess(xml)) {
				options.onSuccess(httpData(xml, options.type));
			} else {
				options.onError();
			}

			options.onComplete();

			hideIndicator();

			xml = null;
		}
	};

	if (xml.contentType) {
		xml.setRequestHeader("Content-Type", data.contentType);
	}

	if (xml.overrideMimeType) {
		xml.setRequestHeader('Connection', 'close');
	}

	xml.send(options.data);

	function httpSuccess(r) {
		try {
			return !r.status && location.protocol == 'file:' || (r.status >= 200 && r.status < 300) || r.status == 304 || navigator.userAgent.indexOf('Safari') >= 0 && typeof r.status == 'undefined';
		} catch (e) {
		}

		return false;
	}

	function httpData(r, type) {
		var ct = r.getResponseHeader('content-type');
		var data = !type && ct && ct.indexOf('xml') >= 0;
		data = type == 'xml' || data ? r.responseXML : r.responseText;

		if (type == 'script') {
			eval.call(window, data);
		}

		return data;
	}

	function showIndicator() {
		if (options.showIndicator && !document.getElementById('ajaxIndicator')) {
			var oIndicator = document.createElement('div');
			oIndicator.innerHTML = options.indicator;
			oIndicator.id = 'ajaxIndicator';
			
			oIndicator.style.position = 'absolute';
			oIndicator.style.right = '5px';
			oIndicator.style.top = '5px';
			oIndicator.style.fontSize = '12px';
			oIndicator.style.padding = '2px';
			
			document.body.appendChild(oIndicator);
		}
	}

	function hideIndicator() {
		if (options.showIndicator) {
			var oIndicator = document.getElementById('ajaxIndicator');
			if (oIndicator) {
				oIndicator.parentNode.removeChild(oIndicator);
			}
		}
	}
}